package com.report.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.convert.Convert;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.report.dto.response.OrderRes;
import com.report.service.IOrderService;
import com.report.socket.PaySocketServer;
import com.zmh.framework.pay.service.impl.AlPay;
import com.zmh.framework.pay.service.impl.WxPay;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/4 19:27
 *  支付回调
 */
@RestController
@RequestMapping(value = "/pay")
@AllArgsConstructor
@SaIgnore
@Slf4j
public class WebPayController {

    private final WxPay wxPay;

    private final AlPay alPay;

    private final IOrderService orderService;

    private final PaySocketServer paySocketServer;

    @PostMapping(value = "/wxNotify")
    public void wxNotify(@RequestBody String xmlData){
        log.info("xmlData:{}",xmlData);
        WxPayOrderNotifyResult wxPayOrderNotifyResult = wxPay.notifyResult(xmlData);
        String orderNo = wxPayOrderNotifyResult.getOutTradeNo();
        OrderRes orderRes = orderService.updateOrderState(orderNo);
        paySocketServer.sendMessage(orderRes);
    }


    @PostMapping(value = "/aliPay")
    public void aliPay(HttpServletRequest request){
        Map<String, Object> map = alPay.notifyResult(request);
        String orderNo = Convert.convert(String.class, map.get("orderNo"));
        OrderRes orderRes = orderService.updateOrderState(orderNo);
        paySocketServer.sendMessage(orderRes);
    }
}
